home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / blackbox.el < prev    next >
Lisp/Scheme  |  1996-01-20  |  15KB  |  422 lines

  1. ;;; blackbox.el --- blackbox game in Emacs Lisp
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
  4.  
  5. ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
  6. ;; Adapted-By: ESR
  7. ;; Keywords: games
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
  29. ;; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89
  30. ;; interface improvements by ESR, Dec 5 1991.
  31.  
  32. ;; The object of the game is to find four hidden balls by shooting rays
  33. ;; into the black box.  There are four possibilities: 1) the ray will
  34. ;; pass thru the box undisturbed, 2) it will hit a ball and be absorbed,
  35. ;; 3) it will be deflected and exit the box, or 4) be deflected immediately,
  36. ;; not even being allowed entry into the box.
  37. ;; 
  38. ;; The strange part is the method of deflection.  It seems that rays will
  39. ;; not pass next to a ball, and change direction at right angles to avoid it.
  40. ;; 
  41. ;;                      R   3   
  42. ;;          1 - - - - - - - - 1 
  43. ;;            - - - - - - - -   
  44. ;;            - O - - - - - - 3 
  45. ;;          2 - - - - O - O -   
  46. ;;          4 - - - - - - - - 
  47. ;;          5 - - - - - - - - 5 
  48. ;;            - - - - - - - - R 
  49. ;;          H - - - - - - - O   
  50. ;;            2   H 4       H   
  51. ;; 
  52. ;; Rays which enter and exit are numbered.  You can see that rays 1 & 5 pass
  53. ;; thru the box undisturbed. Ray 2 is deflected by the northwesternmost
  54. ;; ball.  Likewise rays 3 and 4. Rays which hit balls and are absorbed are
  55. ;; marked with H.  The bottom of the left and the right of the bottom hit
  56. ;; the southeastern ball directly.  Rays may also hit balls after being
  57. ;; reflected. Consider the H on the bottom next to the 4.  It bounces off
  58. ;; the NW-ern most ball and hits the central ball.  A ray shot from above
  59. ;; the right side 5 would hit the SE-ern most ball.  The R beneath the 5
  60. ;; is because the ball is returned instantly.  It is not allowed into
  61. ;; the box if it would reflect immediately.  The R on the top is a more
  62. ;; leisurely return.  Both central balls would tend to deflect it east
  63. ;; or west, but it cannot go either way, so it just retreats.
  64. ;;
  65. ;; At the end of the game, if you've placed guesses for as many balls as
  66. ;; there are in the box, the true board position will be revealed.  Each
  67. ;; `x' is an incorrect guess of yours; `o' is the true location of a ball.
  68.  
  69. ;;; Code:
  70.  
  71. (defvar blackbox-mode-map nil "")
  72.  
  73. (if blackbox-mode-map
  74.     ()
  75.   (setq blackbox-mode-map (make-keymap))
  76.   (suppress-keymap blackbox-mode-map t)
  77.   (define-key blackbox-mode-map "\C-f" 'bb-right)
  78.   (define-key blackbox-mode-map [right] 'bb-right)
  79.   (define-key blackbox-mode-map "\C-b" 'bb-left)
  80.   (define-key blackbox-mode-map [left] 'bb-left)
  81.   (define-key blackbox-mode-map "\C-p" 'bb-up)
  82.   (define-key blackbox-mode-map [up] 'bb-up)
  83.   (define-key blackbox-mode-map "\C-n" 'bb-down)
  84.   (define-key blackbox-mode-map [down] 'bb-down)
  85.   (define-key blackbox-mode-map "\C-e" 'bb-eol)
  86.   (define-key blackbox-mode-map "\C-a" 'bb-bol)
  87.   (define-key blackbox-mode-map " " 'bb-romp)
  88.   (define-key blackbox-mode-map [insert] 'bb-romp)
  89.   (define-key blackbox-mode-map "\C-m" 'bb-done)
  90.   (define-key blackbox-mode-map [kp-enter] 'bb-done))
  91.  
  92. ;; Blackbox mode is suitable only for specially formatted data.
  93. (put 'blackbox-mode 'mode-class 'special)
  94.  
  95. (defun blackbox-mode ()
  96.   "Major mode for playing blackbox.  To learn how to play blackbox,
  97. see the documentation for function `blackbox'.
  98.  
  99. The usual mnemonic keys move the cursor around the box.
  100. \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively.
  101.  
  102. \\[bb-romp] -- send in a ray from point, or toggle a ball at point
  103. \\[bb-done] -- end game and get score
  104. "
  105.   (interactive)
  106.   (kill-all-local-variables)
  107.   (use-local-map blackbox-mode-map)
  108.   (setq truncate-lines t)
  109.   (setq major-mode 'blackbox-mode)
  110.   (setq mode-name "Blackbox"))
  111.  
  112. ;;;###autoload
  113. (defun blackbox (num)
  114.   "Play blackbox.  Optional prefix argument is the number of balls;
  115. the default is 4.
  116.  
  117. What is blackbox?
  118.  
  119. Blackbox is a game of hide and seek played on an 8 by 8 grid (the
  120. Blackbox).  Your opponent (Emacs, in this case) has hidden several
  121. balls (usually 4) within this box.  By shooting rays into the box and
  122. observing where they emerge it is possible to deduce the positions of
  123. the hidden balls.  The fewer rays you use to find the balls, the lower
  124. your score.
  125.  
  126. Overview of play:
  127.  
  128. \\<blackbox-mode-map>\
  129. To play blackbox, type \\[blackbox].  An optional prefix argument
  130. specifies the number of balls to be hidden in the box; the default is
  131. four.
  132.  
  133. The cursor can be moved around the box with the standard cursor
  134. movement keys.
  135.  
  136. To shoot a ray, move the cursor to the edge of the box and press SPC.
  137. The result will be determined and the playfield updated.
  138.  
  139. You may place or remove balls in the box by moving the cursor into the
  140. box and pressing \\[bb-romp].
  141.  
  142. When you think the configuration of balls you have placed is correct,
  143. press \\[bb-done].  You will be informed whether you are correct or
  144. not, and be given your score.  Your score is the number of letters and
  145. numbers around the outside of the box plus five for each incorrectly
  146. placed ball.  If you placed any balls incorrectly, they will be
  147. indicated with `x', and their actual positions indicated with `o'.
  148.  
  149. Details:
  150.  
  151. There are three possible outcomes for each ray you send into the box:
  152.  
  153.     Detour: the ray is deflected and emerges somewhere other than
  154.         where you sent it in.  On the playfield, detours are
  155.         denoted by matching pairs of numbers -- one where the
  156.         ray went in, and the other where it came out.
  157.  
  158.     Reflection: the ray is reflected and emerges in the same place
  159.         it was sent in.  On the playfield, reflections are
  160.         denoted by the letter `R'.
  161.  
  162.     Hit:    the ray strikes a ball directly and is absorbed.  It does
  163.         not emerge from the box.  On the playfield, hits are
  164.         denoted by the letter `H'.
  165.  
  166. The rules for how balls deflect rays are simple and are best shown by
  167. example.
  168.  
  169. As a ray approaches a ball it is deflected ninety degrees.  Rays can
  170. be deflected multiple times.  In the diagrams below, the dashes
  171. represent empty box locations and the letter `O' represents a ball.
  172. The entrance and exit points of each ray are marked with numbers as
  173. described under \"Detour\" above.  Note that the entrance and exit
  174. points are always interchangeable.  `*' denotes the path taken by the
  175. ray.
  176.  
  177. Note carefully the relative positions of the ball and the ninety
  178. degree deflection it causes.
  179.  
  180.     1                                            
  181.   - * - - - - - -         - - - - - - - -         - - - - - - - -       
  182.   - * - - - - - -         - - - - - - - -         - - - - - - - -       
  183. 1 * * - - - - - -         - - - - - - - -         - O - - - - O -       
  184.   - - O - - - - -         - - O - - - - -         - - * * * * - -
  185.   - - - - - - - -         - - - * * * * * 2     3 * * * - - * - -
  186.   - - - - - - - -         - - - * - - - -         - - - O - * - -      
  187.   - - - - - - - -         - - - * - - - -         - - - - * * - -       
  188.   - - - - - - - -         - - - * - - - -         - - - - * - O -       
  189.                                 2                         3
  190.  
  191. As mentioned above, a reflection occurs when a ray emerges from the same point
  192. it was sent in.  This can happen in several ways:
  193.  
  194.